package net.vinote.smart.platform.module.database;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库表对象
 */
public class Table {
	private String tableName;
	List<Field> fields;

	public Table(String tableName) {
		this.tableName = tableName;
		fields = new ArrayList<Field>();
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public List<Field> getFields() {
		return fields;
	}

	public void setFields(List<Field> fields) {
		this.fields = fields;
	}

	/** 已字符串数组的形式返回该表的字段 */
	public String[] getFieldsName() {
		int fieldSize = fields.size();
		String[] fieldArray = new String[fieldSize];
		for (int i = 0; i < fieldSize; i++) {
			fieldArray[i] = fields.get(i).getFieldName();
		}
		return fieldArray;
	}

	public List<Field> getKey() {
		List<Field> keyList = new ArrayList<Field>();
		for (Field field : fields) {
			if (field.isKey()) {
				keyList.add(field);
			}
		}
		return keyList;
	}

	/**
	 * fieldName是否为该表中的主键
	 * @param fieldName
	 * @return
	 */
	public boolean isKey(String fieldName) {
		boolean isKey = false;
		for (Field field : fields) {
			if (field.getFieldName().equals(fieldName)) {
				isKey = field.isKey();
				break;
			}
		}
		return isKey;
	}
}
